using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ItemEditorMenu : MonoBehaviour {
	
	public UILabel itemName;
	
	public UILabel itemDescription;
	
	public UILabel weight;
	
	public UILabel worth;
	public UIPopupList currency;
	
	public GameObject SlotButton;
	public UILabel SlotLabel;
	
	public UICheckbox HasCharges;
	public UILabel TotalCharges;
	public GameObject chargesContainer;
	
	public GameObject AddModifierButton;
	
	public GameObject CancelButton;
	public GameObject AcceptButton;
	
	List<string> buffs;
	List<GameObject> buffObjects;
	
	public UITable holder;
	
	public GameObject target;
	public string functionName;
	
	string origName;
	
	static GameReference _reference;
	
	static GameReference reference
	{
		get
		{
			if(_reference == null)
				_reference = GameObject.FindObjectOfType(typeof(GameReference)) as GameReference;
			return _reference;
		}
	}
	
	bool Loaded = false;

	// Use this for initialization
	void Start () {	
		UIButtonMessage message = AddModifierButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "AddModifierClicked";
		
		message = SlotButton.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "SelectSlot";
		
		if(!Loaded)
		{			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			
			message = AcceptButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "AcceptButtonClicked";
			
			buffs = new List<string>();
			buffObjects = new List<GameObject>();
		}
		
		holder.repositionNow = true;
	}
	
	IEnumerator SelectSlot()
	{
		List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
		{
			new SetterWindowDescription("", "Slot", false, InventoryItem.validSlots)
		};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow("Equipment Slot?", descVar));
		
		if(!string.IsNullOrEmpty(descVar[0].outVariable))
		{
			SlotLabel.text = descVar[0].outVariable;
		}
	}
	
	public void LoadItemDisplay(string iName, bool noEdit = false)
	{
		buffs = new List<string>();
		buffObjects = new List<GameObject>();
		
		InventoryItem i = ItemManager.Load(iName);
		
		origName = iName;
		
		itemName.text = iName;
		itemDescription.text = i.description;
		weight.text = i.weight.ToString();
		
		float w = i.worth;
		if(w < 0.1f)
		{
			w *= 100;
			currency.selection = "cp";
		}
		else if(w < 1)
		{
			w *= 10;
			currency.selection = "sp";
		}
		worth.text = w.ToString();
		
		SlotLabel.text = i.slot;
		HasCharges.isChecked = i.HasCharges;
		TotalCharges.text = i.chargesRemaining.ToString();	
		
		Loaded = true;
		
		CreateBuffObjects(i.buffs.ConvertAll<string>(e => e.name));
		
		if(noEdit)
		{
			NGUITools.AddWidgetCollider(gameObject);
			
			foreach(Collider l in gameObject.GetComponentsInChildren<Collider>())
			{
				l.enabled = false;
			}
			
			collider.enabled = true;
			UIDragObject dragger = gameObject.AddComponent<UIDragObject>();
			dragger.target = holder.transform;
			dragger.restrictWithinPanel = true;
			dragger.scale = Vector3.up;
			dragger.scrollWheelFactor = 1;
			
			CancelButton.SetActiveRecursively(false);
			AddModifierButton.SetActiveRecursively(false);
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelButtonClicked";
			message.collider.enabled = true;
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Some Info";
			}
			
			UIButtonMessage message = AcceptButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
		}
	}
	
	void DeleteModifierListLabel(GameObject target)
	{
		Debug.Log("Deleting: " + target);
		target.name = target.name.Remove(0,3);
		buffs.Remove(target.name);
		Destroy(target);
		
		holder.repositionNow = true;
	}
	
	void AddModifierClicked()
	{
		BrowserManager.CreateBuffsBrowser(gameObject, "CreateBuffObjects", buffs, false);
	}
	
	void CreateBuffObjects(List<string> selections)
	{
		if(selections != null)
			buffs = selections;
		
		foreach(GameObject buff in buffObjects)
		{
			Destroy(buff);
		}
		
		buffs.Sort();
		for(int i = 0; i < buffs.Count; i++)
		{
			GameObject g = Instantiate(reference.SkillLabelPrefab) as GameObject;
			g.name = "g " + (i+2) + " BuffLabel";
			g.transform.parent = AddModifierButton.transform.parent.parent;
			g.active = true;
			g.transform.localScale = Vector3.one;
			g.transform.position = AddModifierButton.transform.position;
			
			BuffDebuff b = BuffDebuffManager.Load(buffs[i]);
			
			for(int ii = 0; ii < b.modifiers.Count; ii++)
			{
				TemporaryBonus tb = b.modifiers[ii];
				UILabel bonusLabel = NGUITools.AddWidget<UILabel>(g);
				bonusLabel.transform.localScale = Vector3.one * 18;
				bonusLabel.transform.localPosition = Vector3.down * 20 * ii + Vector3.down * 20 + Vector3.left * 120;
				bonusLabel.text = tb.GetDescription();
				bonusLabel.lineWidth = 185;
				bonusLabel.multiLine = true;
				bonusLabel.pivot = UIWidget.Pivot.Left;
			}
			
			g.GetComponentInChildren<UIDragObject>().target = holder.transform;
			
			g.GetComponentInChildren<UILabel>().text = buffs[i];
			buffObjects.Add(g);
		}
		
		holder.repositionNow = true;
	}
	
	void SaveChangesClicked()
	{
		InventoryItem i = new InventoryItem();
		i.name = itemName.text;
		i.description = itemDescription.text;
		i.buffs = new List<BuffDebuff>();
		i.weight = float.Parse(weight.text);
		i.worth = float.Parse(worth.text);
		i.slot = SlotLabel.text;
		i.HasCharges = HasCharges.isChecked;
		i.chargesRemaining = int.Parse(TotalCharges.text);		
		
		Debug.Log(currency.selection);
		
		if(currency.selection == "sp")
			i.worth /= 10;
		else if(currency.selection == "cp")
			i.worth /= 100;
		
		foreach(string b in buffs)
		{
			i.buffs.Add(BuffDebuffManager.Load(b));
		}
		
		if(ItemManager.SaveItem(i, origName))
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, i.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelButtonClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete this item?", b));
		
		if(b[0])
		{
			ItemManager.DeleteItem(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}
		 
		Destroy(gameObject);
		yield break;
	}
	
	void AcceptButtonClicked()
	{
		InventoryItem i = new InventoryItem();
		i.name = itemName.text;
		i.description = itemDescription.text;
		i.buffs = new List<BuffDebuff>();
		i.weight = float.Parse(weight.text);
		i.worth = float.Parse(worth.text);
		
		i.slot = SlotLabel.text;
		i.HasCharges = HasCharges.isChecked;
		i.chargesRemaining = int.Parse(TotalCharges.text);		
		
		if(currency.selection == "sp")
			i.worth /= 10;
		else if(currency.selection == "cp")
			i.worth /= 100;
		
		foreach(string b in buffs)
		{
			i.buffs.Add(BuffDebuffManager.Load(b));
		}
		
		i = ItemManager.AddItem(i.name, i.description, i.weight, i.buffs, i.worth, i.slot, 1, i.HasCharges, i.chargesRemaining);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(i.name+"$", i.description), SendMessageOptions.DontRequireReceiver);
		
		Destroy(gameObject);
	}
}
